
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:my_flutter/first/ui/page_login.dart';

import '../app_manager.dart';
import '../event/events.dart';
import 'page_collect.dart';

class DrawerContent extends StatefulWidget {
  const DrawerContent({super.key});

  @override
  State<DrawerContent> createState() => _DrawerContentState();
}

class _DrawerContentState extends State<DrawerContent> {
  User? currentUser;
  bool? isLogin;

  @override
  void initState() {
    super.initState();
    isLogin = AppManager.prefs?.getBool(AppManager.LOGIN_STATE);
    debugPrint("DrawerContent isLogin:$isLogin");
    if (isLogin == null) {
      isLogin = false;
      currentUser = null;
      AppManager.prefs?.setString(AppManager.USER_NAME, "");
      AppManager.prefs?.setString(AppManager.USER_PASSWORD, "");
      AppManager.prefs?.setBool(AppManager.LOGIN_STATE, false);
    } else if (isLogin == false) {
      currentUser = null;
    } else {
      currentUser = User("", "");
      currentUser!.userName = AppManager.prefs?.getString(AppManager.USER_NAME);
      currentUser!.userName = AppManager.prefs?.getString(AppManager.USER_NAME);
    }
    AppManager.eventBus.on<LoginEvent>().listen((data) {
      debugPrint(data.runtimeType.toString());
      currentUser = User((data).username, (data).password);
      isLogin = true;
          if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    Widget userHeader = DrawerHeader(
      decoration: BoxDecoration(color: Colors.blue),
      child: InkWell(
        onTap: () => _itemClick(null),
        child: Column(
          children: [
            Padding(
              padding: EdgeInsets.only(bottom: 18.0),
              child: CircleAvatar(
                backgroundImage: AssetImage("assets/images/logo.png"),
                radius: 38.0,
              ),
            ),
            Text(
              currentUser?.userName ?? "请先登录",
              style: TextStyle(color: Colors.white, fontSize: 18.0),
            ),
          ],
        ),
      ),
    );
    return ListView(
      padding: EdgeInsets.zero,
      children: [
        userHeader,
        InkWell(
          onTap: () => _itemClick(CollectPage()),
          child: ListTile(
            leading: Icon(Icons.favorite),
            title: Text('收藏列表', style: TextStyle(fontSize: 16.0)),
          ),
        ),

        ///分隔线
        Padding(
          padding: EdgeInsets.fromLTRB(18.0, 0.0, 18.0, 0.0),
          child: Divider(color: Colors.grey),
        ),

        ///退出登录
        Offstage(
          offstage: isLogin == false,
          child: InkWell(
            onTap: () {
              isLogin = false;
              AppManager.isLogin = false;
              AppManager.prefs?.setBool(AppManager.LOGIN_STATE, false);
              currentUser = null;
            },
            child: ListTile(
              leading: Icon(Icons.exit_to_app),
              title: Text(currentUser?.userName ?? "退出登录", style: TextStyle(fontSize: 16.0)),
            ),
          ),
        ),
      ],
    );
  }

  void _itemClick(Widget? page) {
    var dstPage = currentUser == null ? LoginPage() : page;
    if (dstPage != null) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) {
            return dstPage;
          },
        ),
      );
    }
  }
}

class User {
  String? userName;
  String? userIcon;

  User(this.userName, this.userIcon);
}
